<template>
  <div calss="upload">
    <el-upload
      action="/couponcenter/file/upload"
      :class="imgs?'avatar-uploaderAA':'avatar-uploader'"
      :show-file-list="false"
      accept=".jpg, .jpeg, .png"
      :auto-upload="true"
      :disabled="imgs != '' || disableds"
      :on-success="handUploads"
      :on-error="handError"
      v-loading="isLoading"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus" v-if="!imgs"></i>
      <img v-if="imgs" :src="imgs" class="avatar" />
      <div v-if="imgs" class="el-upload-list__item-actions"  @dblclick="handlePictureCardPreviews(imgs)">
        <div class="icon-center">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreviews(imgs)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>
          <span
            class="el-upload-list__item-preview"
            @click="handleRemovess()"
            v-if="isEdit"
          >
            <i class="el-icon-delete"></i>
          </span>
        </div>
      </div>
      <div class="el-upload__tip" slot="tip" v-if="tip">{{tip}}</div>
    </el-upload>
    <el-image-viewer
           v-if="showViewer"
           :on-close="closeViewer"
           :url-list="srcList" />
  </div>
</template>

<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  components: { ElImageViewer },
  props:["imgs","isEdit","disableds","tip"],
  data() {
    return {
      srcList:[],
      showViewer:false,
      isLoading:false
    };
  },
  methods: {
    beforeUpload(file){
      this.isLoading = true
      const imageType = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/jpeg';
      if (!imageType) {
        this.$message.error('上传图片只能为jpg或png格式');
        this.isSuccess = false
        // this.isLoading = true
      }else{
        this.isSuccess = true
      }
    },
    handUploads(res, file) {
      this.isLoading = false
      if(this.isSuccess){
        this.$emit('handleRechargeVoucher',res.data)
        this.srcList[0] = res.data;
      }
      // this.imgs = res.data;
    },
    handError(){
      this.isLoading = false
      this.$message.error('服务器异常，请联系管理员')
    },
    handlePictureCardPreviews(val) {
      // console.log(val)
      this.srcList[0] = val 
      this.showViewer = true
    },
    closeViewer() {
     this.showViewer = false
    },
    handleRemovess() {
      this.$confirm("是否确定移除该图片？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$emit('handleRechargeVoucher',"")
        this.srcList[0] = ""
      });
    },
  },
};
</script>

<style lang="scss" >
// 预览样式
.el-image-viewer__wrapper{
  z-index: 9999 !important;
  height:100%;
}

.avatar-uploaderAA {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 178px;
    height: auto;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .el-upload:hover {
    border-color: #409eff;
  }
  .el-icon-plus {
    font-size: 28px;
    color: #8c939d;
  }
  .avatar {
    width: 100%;
    height: 100%;
  }
  //   按钮
  .el-upload-list__item-actions:hover {
    opacity: 1;
  }
  .el-upload-list__item-actions {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    cursor: default;
    text-align: center;
    color: #fff;
    opacity: 0;
    font-size: 20px;
    background-color: rgba(0, 0, 0, 0.5);
    transition: opacity 0.3s;
  }
  .icon-center {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
    justify-content: space-evenly;
    padding: 0 40px;
    span {
      cursor: pointer;
      font-size: 20px;
    }
  }
}
.avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 178px;
    height: 178px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .el-upload:hover {
    border-color: #409eff;
  }
  .el-icon-plus {
    font-size: 28px;
    color: #8c939d;
  }
  .avatar {
    width: 100%;
    height: 100%;
  }
  //   按钮
  .el-upload-list__item-actions:hover {
    opacity: 1;
  }
  .el-upload-list__item-actions {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    cursor: default;
    text-align: center;
    color: #fff;
    opacity: 0;
    font-size: 20px;
    background-color: rgba(0, 0, 0, 0.5);
    transition: opacity 0.3s;
  }
  .icon-center {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
    justify-content: space-evenly;
    padding: 0 40px;
    span {
      cursor: pointer;
      font-size: 20px;
    }
  }
}
.el-image-viewer__mask {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    opacity: .9;
    background: #000;
}
</style>
